import 'package:base_network/export.dart';
import 'package:base_widgets/export.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_teach/common/common_util.dart';
import 'package:flutter_teach/custom_layout/page_test.dart';
import 'package:flutter_teach/use_provider/page_add_num.dart';
import 'package:flutter_teach/pages/register/page_register.dart';
import '/pages/main/page_main.dart';
import 'constant/app_router.dart';
import 'network/app_api_service.dart';
import 'network/login_check_interceptor.dart';
import 'network/status_check_interceptor.dart';
import 'pages/login/page_login.dart';
import 'pages/mine/page_plugin.dart';
import 'web/page_web.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Provider.debugCheckInvalidValueType = null;
  AppApiService.init(
    "https://www.wanandroid.com",
    interceptors: [
      CookieManager(await CommonUtil.getCookieJar()),
      AppLogInterceptor(),
      LoginCheckInterceptor(),
      StatusCheckInterceptor(),
    ],
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MainPage(),
      initialRoute: "/",
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case AppRouter.home:
            return MaterialPageRoute(builder: (context) => const MainPage());
          case AppRouter.login:
            return MaterialPageRoute(builder: (context) => const LoginPage());
          case AppRouter.register:
            return MaterialPageRoute(builder: (context) => const RegisterPage());
          case AppRouter.plugin:
            return MaterialPageRoute(builder: (context) => const PluginPage());
          case AppRouter.provider:
            return MaterialPageRoute(builder: (context) => const AddNumPage());
          case AppRouter.web:
            Map argsMap = settings.arguments as Map;
            return MaterialPageRoute(
              builder: (context) => WebPage(
                loadUrl: argsMap["loadUrl"],
                title: argsMap["title"],
              ),
            );
          case AppRouter.myCenterWidget:
            return MaterialPageRoute(builder: (context) => const TestMyCenterPage());
        }
        return null;
      },
    );
  }
}
